package com.eqdd.yiqidian.base;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;

import com.eqdd.yiqidian.bean.User;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.http.JsonCallBack;
import com.eqdd.yiqidian.ui.LoginActivity;
import com.eqdd.yiqidian.ui.MineAuActivity;
import com.eqdd.yiqidian.utils.EncryptUtil;
import com.eqdd.yiqidian.utils.GreenDaoUtil;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ChooseAlertView;
import com.eqdd.yiqidian.widget.EditDialogFragment;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

import java.util.HashMap;
import java.util.List;

/**
 * Created by lv on 17-9-2.
 */

public class BaseActivityLifeCallBack implements Application.ActivityLifecycleCallbacks {
    private static int mCount = 0;

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {
        AppManager.getAppManager().addActivity(activity);
    }

    @Override
    public void onActivityStarted(Activity activity) {
        mCount++;
        //如果mCount==1，说明是从后台到前台
        if (mCount == 1) {
            //执行app跳转到前台的逻辑
            if (AppConstent.user == null) {
                AppConstent.user = GreenDaoUtil.getUser();
            }
            if (AppConstent.user != null) {
                HashMap<String, String> map = new HashMap<>();
                map.put("u1", AppConstent.user.getUname());
                map.put("u2", EncryptUtil.sha1(SPUtil.getParam(Config.PASS) + "EQD"));
                OkGo.<HttpResult<User>>post(HttpConfig.BASE_URL + HttpConfig.LOGIN)
                        .params(map)
                        .execute(new JsonCallBack<HttpResult<User>>() {
                            @Override
                            public void onSuccess(Response<HttpResult<User>> response) {
                                HttpResult<User> httpResult = response.body();
                                if (httpResult.getStatus() == 200) {
                                    GreenDaoUtil.insert(httpResult.getItems());
                                    AppConstent.user = GreenDaoUtil.getUser();
                                } else {
                                    AlertDialog alertDialog = ChooseAlertView.getAlertDialog(activity, "提醒", "身份校验失败,请重新登陆", new EditDialogFragment.OnClickListener() {
                                        @Override
                                        public void onClick(List<String> contents) {
                                            GreenDaoUtil.deleteAllUser();
                                            Intent intent1 = new Intent(activity, LoginActivity.class);
                                            activity.startActivity(intent1);
                                            activity.finish();
                                        }
                                    });
                                    alertDialog.show();
                                }
                            }
                        });
            }

        }
    }

    @Override
    public void onActivityResumed(Activity activity) {

    }

    @Override
    public void onActivityPaused(Activity activity) {

    }

    @Override
    public void onActivityStopped(Activity activity) {
        mCount--;
        //如果mCount==0，说明是前台到后台
        if (mCount == 0) {
            //执行应用切换到后台的逻辑
        }
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
        AppManager.getAppManager().finishActivity(activity);
    }
}
